package com.hxzy.commons.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hxzy.commons.dao.ICacheDao;
import com.hxzy.commons.model.CmsCodeData;
import com.hxzy.commons.model.CmsCodedataId;
import com.hxzy.commons.service.ICacheService;
import com.hxzy.core.base.dao.IBaseDAO;
import com.hxzy.core.base.service.impl.BaseService;
import com.hxzy.core.page.Finder;
import com.hxzy.core.page.entity.PageParams;
import com.hxzy.core.page.entity.Pagination;

@Service
public class CacheServiceImpl extends BaseService implements ICacheService{
	@Autowired
	private ICacheDao cacheDao;
	
	/**
	 * 加载字典集列表
	 * @param pageParams
	 * @return
	 */
	@Override
	public Pagination loadDicttList(PageParams pageParams,String codetype){
		Finder finder = Finder.create("from CmsCodeIndex");
		if(StringUtils.isNotBlank(codetype)){
			finder.append(" where codetype like:codetype"); 
			finder.setParam("codetype", "%" + codetype + "%");
		}
		finder.append(" order by startdate desc");
		Pagination page = cacheDao.find(finder, pageParams.getPageNo(), pageParams.getPageSize());
		return page;
	}
	
	@Override
	public IBaseDAO getBaseDao() {
		return this.cacheDao;
	}
	
	@Override
	public List getData4Tree(String treeType) {
		String sql = "select '0' as pId,a.CODETYPE as id,a.CODETYPE as name,'1' as level,'icon01' as iconSkin from cms_codeindex a where a.STATUS=1 ";
		List list = cacheDao.queryListBySql(sql, null);
		return list;
	}

	@Override
	public Object getOneDictData(String codetype, String codevalue) {
		String hql = "from CmsCodeData as a where a.id.codetype = :codetype and a.id.codevalue = :codevalue";
		String[] argnames = {"codetype","codevalue"};
		String[] args = {codetype,codevalue};
		CmsCodeData data = (CmsCodeData)cacheDao.getObjectbyHql(hql, argnames, args);
		return data;
	}

	@Override
	public void deleteMoreDictData(String codetype) {
		String sql = "delete from cms_codedata where 1=1 and codetype=?";
		cacheDao.deletObjectbySql(sql, new String[]{codetype});
	}

	@Override
	public Pagination loadDictDataList(PageParams pageParams, CmsCodedataId id) {
		Finder finder = Finder.create("from CmsCodeData where 1=1");
		if(StringUtils.isNotBlank(id.getCodetype())){
			finder.append(" and id.codetype = :codetype"); 
			finder.setParam("codetype", id.getCodetype());
		}
		if(StringUtils.isNotBlank(id.getCodevalue())){
			finder.append(" and id.codevalue = :codevalue"); 
			finder.setParam("codevalue", id.getCodevalue());
		}
		finder.append(" order by startdate desc");
		Pagination page = cacheDao.find(finder, pageParams.getPageNo(), pageParams.getPageSize());
		return page;
	}
}
